package kinect_biomec.Logic;

import Testes.SimpleOpenNI;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ControlKinect {

    public KinectView kinectView = new KinectView();
    public KinectView3D kinectView3D = new KinectView3D();
    private SimpleOpenNI context = null;

    public SimpleOpenNI getContext() {
        return kinectView.context;
    }

    public void setContext(SimpleOpenNI context) {
        kinectView.setContext(context);
    }

    public ControlKinect() {
        kinectView.init();
    }

    public void sleep(int time) {
        synchronized (this) {
            try {
                wait(time);
            } catch (InterruptedException ex) {
                Logger.getLogger(ControlKinect.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    boolean jaFoiAtivadoScene = false;

    public void restartContext(){
        //context.close();
        //context = new SimpleOpenNI(kinectView, SimpleOpenNI.RUN_MODE_MULTI_THREADED);
        
        SimpleOpenNI.start();
        context.initVars();
        context.setupCallbackFunc();
        //context.init();
       
    }
    public void println(String msg){
        System.out.println(msg);
    }
    public void recorderPlayContext(boolean recording){
       // context = new SimpleOpenNI(kinectView);
        
        boolean recordFlag = recording;
        if (recordFlag == false) {
            // playing, this works without the camera
            if (context.openFileRecording("test.oni") == false) {
                println("can't find recording !!!!");
                //exit();
            }

            // it's possible to run the sceneAnalyzer over the recorded data strea   
            if (context.enableScene() == false) {
                println("can't setup scene!!!!");
                //exit();
                return;
            }

            println("This file has " + context.framesPlayer() + " frames.");
        } else {

            context.enableRecorder(SimpleOpenNI.RECORD_MEDIUM_FILE, "test.oni");

            // select the recording channels
            context.addNodeToRecording(SimpleOpenNI.NODE_DEPTH,
                    SimpleOpenNI.CODEC_16Z_EMB_TABLES);
            context.addNodeToRecording(SimpleOpenNI.NODE_IMAGE,
                    SimpleOpenNI.CODEC_JPEG);
           
        }


        /*// set window size 
        if ((context.nodes() & SimpleOpenNI.NODE_DEPTH) != 0) {
            if ((context.nodes() & SimpleOpenNI.NODE_IMAGE) != 0) // depth + rgb 
            {
                ///size(context.depthWidth() + 10 + context.rgbWidth(),
                       // context.depthHeight() > context.rgbHeight() ? context.depthHeight() : context.rgbHeight());
            } else // only depth
            {
               // size(context.depthWidth(), context.depthHeight());
            }
        } else {
           // exit();
        }*/
    }
    
    public void createContext(boolean depth, boolean rgb,boolean mirror ,boolean scene,boolean recording){
      /*  System.gc();
        sleep(1000);*/
        
        
        
        if(context==null){
           context = new SimpleOpenNI(kinectView, SimpleOpenNI.RUN_MODE_SINGLE_THREADED);
        }else{
            restartContext();
        }
        
        if(recording){
            recorderPlayContext(recording);
        }
        
        if (depth) {
            context.enableDepth();
        }
        
        kinectView.setShowingDepth(depth);


        if (rgb) {
            context.enableRGB();
        }
        kinectView.setShowingRGB(rgb);


        if (scene) {
            context.enableScene();
        }
       
        context.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL);
        context.setMirror(mirror);
    }
    
    public void initContext(boolean depth, boolean rgb, boolean mirror, boolean scene, boolean justUser, int skeleton,
            boolean centroMass2D,boolean centroMass3D,boolean centroMassMembros,boolean recording) {
        
 
        createContext(depth, rgb, mirror, scene,recording);
        
        kinectView.setShowingDepth(depth);



        kinectView.setShowingScene(scene);

        kinectView.setShowJustUser(justUser);
        
        
        kinectView.setSkelProfile(skeleton);


        kinectView.setCalcPontoDeMass2D(centroMass2D);
        kinectView.setCalcPontoDeMass3D(centroMass3D);
        kinectView.setCalcPontoDeMassMembros(centroMassMembros);
        
        kinectView.setContext(context);

    }
    
    public void startViewKinect(){
        kinectView = new KinectView();
        kinectView.init();
    }
    public void startViewKinect3D(){
        kinectView3D = new KinectView3D();
        kinectView3D.init();
    }
    
    public void initContext3D(boolean mirror, boolean justUser, int skeleton,
            boolean centroMass3D) {
        
        
        createContext(true,false ,mirror,false,false);
        kinectView3D.setCalcPontoDeMass3D(centroMass3D);
        kinectView3D.setSkelProfile(skeleton);
        kinectView3D.setContext(context);
        
    }
    
    public void abortCalibration(){
        context.abortCalibrationSkeleton(1);
        context.clearCalibrationDataSkeleton(1);
    }
            
    public void setSmothing(float i){
        context.setSmoothingSkeleton(i);
    }
    
    public void createContextOfAFile(){
        
    }
}
